package com.briup.srms.project.service.impl;

import com.briup.srms.project.bean.Group;
import com.briup.srms.project.bean.StudentGroup;
import com.briup.srms.project.bean.StudentGroupExample;
import com.briup.srms.project.bean.extend.GroupExtend;
import com.briup.srms.project.dao.GroupMapper;
import com.briup.srms.project.dao.StudentGroupMapper;
import com.briup.srms.project.dao.extend.GroupExtendMapper;
import com.briup.srms.project.service.IGroupService;
import com.briup.srms.sms.bean.Student;
import com.briup.srms.sms.dao.StudentMapper;
import com.jacky.base.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: sms
 * @description: 毕设分组
 * @author: charles
 * @create: 2021-08-06 10:34
 **/
@Service
public class GroupServiceImpl implements IGroupService {
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentGroupMapper sgMapper;
    @Autowired
    private GroupExtendMapper geMapper;

    @Override
    public void combine(Group group, Long[] studentIds) throws CustomerException {
        group.setStatus("正常");
        group.setCreateTime(new Date().getTime());
        groupMapper.insert(group);

        if(studentIds.length<=3){
            throw new CustomerException("小组成员不应该少于3位");
        }

        for(long studentId : studentIds){
            Student student = studentMapper.selectByPrimaryKey(studentId);
            if(student == null){
                throw new CustomerException("学生信息不存在");
            }
            // 判断学生是否在其他组中
            GroupExtend g = this.queryStudentGroup(studentId);
            if(g!=null){
                throw new CustomerException(student.getRealname()+"已经在其他组内");
            }

            StudentGroup sg = new StudentGroup();
            sg.setGroupId(group.getId());
            sg.setStudentId(studentId);

            studentMapper.insert(student);
        }
    }

    @Override
    public void dissolve(long groupId) throws CustomerException {
        groupMapper.deleteByPrimaryKey(groupId);
    }

    @Override
    public void changeGroup(long studentId, long groupId) throws CustomerException {
        GroupExtend group = this.queryStudentGroup(studentId);
        // 从原来组移除
        StudentGroupExample example = new StudentGroupExample();
        example.createCriteria().andGroupIdEqualTo(groupId).andStudentIdEqualTo(studentId);
        sgMapper.deleteByExample(example);
        // 加入到现有组
        StudentGroup sg = new StudentGroup();
        sg.setStudentId(studentId);
        sg.setGroupId(groupId);
        sgMapper.insert(sg);
    }

    @Override
    public List<GroupExtend> query(Long channelId, Long leaderId) {
        return null;
    }

    @Override
    public GroupExtend queryStudentGroup(long studentId) {
        return null;
    }

    @Override
    public void selectProject(long groupId, long projectId) throws CustomerException {

    }

    @Override
    public void changeProjectName(long groupId, String name) throws CustomerException {

    }

    @Override
    public void delSelectedProject(long groupId) {

    }
}
